package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.Shopinfoes;
import com.base.po.voip.ShopinfoesExample;
import com.base.po.voip.ShopinfoesExample.Criteria;
import com.base.service.LogsService;
import com.base.service.ShopinfoService;
import com.base.utils.IP;

@Controller
@RequestMapping("/admin")
public class content_xiugaibada {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private ShopinfoService shopinfoService;

	// 账户信息
	@RequestMapping("/content_xiugaibada")
	public ModelAndView index(HttpServletRequest request, @RequestParam(value = "id", required = true) String id)
			throws Exception {
		// 返回ModelAndView
		System.out.println(id);
		ModelAndView modelAndView = new ModelAndView();
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		Shopinfoes shopinfoes = shopinfoService.selectByPrimaryKey(id);
		if (shopinfoes == null || shopinfoes.getId() == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}

		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Shopinfoes> shopinfoesList = shopinfoService.selectByExample(shopinfoesExample);

		System.out.println(shopinfoes.toString());
		request.setAttribute("id", id);
		request.setAttribute("url", shopinfoes.getUrl());
		request.setAttribute("text", shopinfoes.getText());
		request.setAttribute("datastatus", shopinfoes.getDatastatus());
		request.setAttribute("grade", shopinfoes.getGrade());
		request.setAttribute("shopinfoesList", shopinfoesList);
		request.setAttribute("logo", shopinfoes.getLogo());

		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
		} else {
			modelAndView.setViewName("jsp/content_xiugaibada");
		}
		return modelAndView;
	}

	String SUCCESS = "success";

	// 更改商户信息
	@RequestMapping(value = "/updateShopinfoes", method = RequestMethod.POST)
	public void updateShopinfoes(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String agentid, String logo, String text,
			@RequestParam(value = "url", required = true) String url, Integer type, Boolean datastatus, Integer grade, Integer move) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		PrintWriter out = response.getWriter();
		
		System.out.println(url);
		
		
		Integer change = 0;
		// 更改grade
		Integer changeGrade = 0;
		// 更改id
		String changeId = null;
		if (move != null) {
			// 前移
			if (grade != null && move == 1) {
				for (int i = 0; i < shopinfoes.size(); i++) {
					if (shopinfoes.get(i).getGrade() == grade) {
						int find = i - 1;
						if (find < 0) {
							out.print("已经是第一张图了");
							return;
						} else {
							if (grade < shopinfoes.get(i - 1).getGrade()) {
								changeId = shopinfoes.get(i - 1).getId();
								changeGrade = shopinfoes.get(i - 1).getGrade();
								change = grade;
								grade = changeGrade;
								changeGrade = change;
								break;
							}
						}
					}
				}
			}

			// 后移
			if (grade != null && move == 2) {
				for (int i = 0; i < shopinfoes.size(); i++) {
					if (shopinfoes.get(i).getGrade() == grade) {
						System.out.println(i + 1);
						int find = i + 1;
						int index = shopinfoes.size() - 1;
						if (find > index) {
							out.print("已经是最后一张图了");
							return;
						} else {
							if (grade > shopinfoes.get(i + 1).getGrade()) {
								changeId = shopinfoes.get(i + 1).getId();
								changeGrade = shopinfoes.get(i + 1).getGrade();
								change = grade;
								grade = changeGrade;
								changeGrade = change;
								break;
							}
						}
					}
				}
			}
		}

		request.setAttribute("shopinfoes", shopinfoes);

		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			Shopinfoes record = new Shopinfoes(id.trim(), agentid, logo, text, url, type, datastatus, grade);
			int code = shopinfoService.updateByPrimaryKeySelective(record);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			if (changeId != null) {
				Shopinfoes changeRecord = new Shopinfoes(changeId, agentid, logo, text, url, type, datastatus,
						changeGrade);
				int changeCode = shopinfoService.updateByPrimaryKeySelective(changeRecord);

				if (code == 1 && changeCode == 1) {
					logsService.insert(CommContent.TYPE_UPDATE, "移动" + text + "模块", usernamesys, true,
							IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_UPDATE, "移动" + text + "模块失败", usernamesys, true,
							IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
			} else {
				if (code == 1) {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + text + "模块", usernamesys, true,
							IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + text + "模块失败", usernamesys, true,
							IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
			}

		}
	}
	
	//删除时
	@RequestMapping(value = "/delectShopinfoes", method = RequestMethod.POST)
	public void delectShopinfoes(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String agentid, String logo, String text,
			String url, Integer type, Boolean datastatus, Integer grade, Integer move) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		System.out.println(adminUserId);
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		
		Shopinfoes sp = shopinfoService.selectByPrimaryKey(id);
		
		PrintWriter out = response.getWriter();
		
		for(Shopinfoes s : shopinfoes) {
			
			if(s.getGrade()< sp.getGrade()) {
				
			}else {
				s.setGrade(s.getGrade()-1);
				shopinfoService.updateByPrimaryKeySelective(s);
			}
		}
		
		

		request.setAttribute("shopinfoes", shopinfoes);

		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			Shopinfoes record = new Shopinfoes(id.trim(), agentid, logo, text, url, type, datastatus, grade);
			int code = shopinfoService.updateByPrimaryKeySelective(record);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			
				if (code == 1) {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + text + "模块", usernamesys, true,
							IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + text + "模块失败", usernamesys, true,
							IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
			

		}
	}

	//移动位置时
	@RequestMapping(value = "/moveShopinfoes", method = RequestMethod.POST)
	public void updateShopinfoesTest2(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String nextid,String agentid, String logo, String text,
			String url, Integer type, Boolean datastatus, Integer grade, Integer move) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		System.out.println(adminUserId);
		ShopinfoesExample shopinfoesExample = new ShopinfoesExample();
		shopinfoesExample.setOrderByClause("Grade DESC");
		Criteria a = shopinfoesExample.createCriteria();
		a.andAgentidEqualTo(adminUserId).andTypeEqualTo(1).andDatastatusEqualTo(true);
		System.out.println(adminUserId);
		List<Shopinfoes> shopinfoes = shopinfoService.selectByExample(shopinfoesExample);
		
		//移动模块的id
		Shopinfoes sp = shopinfoService.selectByPrimaryKey(id);
		//移动模块之后的id
		Shopinfoes spnext = shopinfoService.selectByPrimaryKey(nextid);
		
		PrintWriter out = response.getWriter();
		
		Integer grades = spnext.getGrade();
		
		System.out.println(url);
		
		
		for(Shopinfoes s : shopinfoes) {
			if(s.getGrade()<= spnext.getGrade() && s.getGrade()>sp.getGrade()) {
				if(s.getGrade() != 0) {
					s.setGrade(s.getGrade()-1);
					shopinfoService.updateByPrimaryKeySelective(s);
				}
			}
		}
		
		

		request.setAttribute("shopinfoes", shopinfoes);

		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else {
			Shopinfoes record = new Shopinfoes(id.trim(), agentid, logo, text, url, type, datastatus, grades);
			int code = shopinfoService.updateByPrimaryKeySelective(record);
			String usernamesys = (String) request.getSession().getAttribute("useracc");
			
				if (code == 1) {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + text + "模块", usernamesys, true,
							IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_UPDATE, "修改" + text + "模块失败", usernamesys, true,
							IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
			

		}
	}
}
